package com.jiyi.activity.datainput.jm;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.datainput.DataInputXFSSBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataInputXFSSReadActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.tvSSGZ)
	TextView tvSSGZ;
	@BindView(R.id.tvSSSH)
	TextView tvSSSH;
	@BindView(R.id.tvSSTH)
	TextView tvSSTH;
	@BindView(R.id.tvWSZXFSS)
	TextView tvWSZXFSS;
	@BindView(R.id.tvBFHXFBZ)
	TextView tvBFHXFBZ;
	@BindView(R.id.tvYXFHFYGN)
	TextView tvYXFHFYGN;

	@BindView(R.id.tvSFFHBZWZ)
	TextView tvSFFHBZWZ;
	@BindView(R.id.tvSFFHBZSL)
	TextView tvSFFHBZSL;
	@BindView(R.id.tvSFFHBZZD)
	TextView tvSFFHBZZD;
	@BindView(R.id.tvSSSFFHBZWZ)
	TextView tvSSSFFHBZWZ;
	@BindView(R.id.tvSSSFFHBZSL)
	TextView tvSSSFFHBZSL;
	@BindView(R.id.tvSSSFFHBZZD)
	TextView tvSSSFFHBZZD;

	@BindView(R.id.tvLCSFFHBZWZ)
	TextView tvLCSFFHBZWZ;
	@BindView(R.id.tvLCSFFHBZSL)
	TextView tvLCSFFHBZSL;
	@BindView(R.id.tvLCSFFHBZZD)
	TextView tvLCSFFHBZZD;

	String build_no;
	Dialog dialog;
	
	@BindView(R.id.gvXiu)
	GridView gvXiu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_input_xfss_read);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	public void init() {
		setTop();
		getIntentData();
		getData();
	}

	private void setTop() {
		tvTitle.setText("建筑消防设施");
		tvSearch.setVisibility(View.GONE);
	}

	private void getIntentData() {
		build_no = getIntent().getStringExtra("build_no");
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("build_info_id", build_no);

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.DATA_INPUT_XFSS_READ, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								Log.e("dfdsf   ", result);
								DataInputXFSSBean temp = new Gson().fromJson(
										data, DataInputXFSSBean.class);
								if (temp == null) {
									temp = new DataInputXFSSBean();
									temp.setBuild_info_id(build_no);
									temp.setDamage(0);
									temp.setEmergency_lighting_add(0);
									temp.setEmergency_lighting_bright(0);
									temp.setEmergency_lighting_num(0);
									temp.setEscape_sign_add(0);
									temp.setEscape_sign_bright(0);
									temp.setEscape_sign_num(0);
									temp.setFault(0);
									temp.setFloor_indicator_add(0);
									temp.setFloor_indicator_bright(0);
									temp.setFloor_indicator_num(0);
									temp.setIs_affect_fire_function(0);
									temp.setPalsy(0);
									temp.setSet_fire_fighting_equipment(0);
									temp.setSet_inconformity_standard(0);
								}
								setData(temp);
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	void setData(DataInputXFSSBean temp) {
		if (temp.getFault() == 0) {
			tvSSGZ.setText("否");
		} else {
			tvSSGZ.setText("是");
		}
		if (temp.getDamage() == 0) {
			tvSSSH.setText("否");
		} else {
			tvSSSH.setText("是");
		}
		if (temp.getPalsy() == 0) {
			tvSSTH.setText("否");
		} else {
			tvSSTH.setText("是");
		}
		if (temp.getSet_fire_fighting_equipment() == 0) {
			tvWSZXFSS.setText("否");
		} else {
			tvWSZXFSS.setText("是");
		}
		if (temp.getSet_inconformity_standard() == 0) {
			tvBFHXFBZ.setText("否");
		} else {
			tvBFHXFBZ.setText("是");
		}

		if (temp.getIs_affect_fire_function() == 0) {
			tvYXFHFYGN.setText("否");
		} else {
			tvYXFHFYGN.setText("是");
		}
		if (temp.getEmergency_lighting_add() == 0) {
			tvSFFHBZWZ.setText("否");
		} else {
			tvSFFHBZWZ.setText("是");
		}

		if (temp.getEmergency_lighting_num() == 0) {
			tvSFFHBZSL.setText("否");
		} else {
			tvSFFHBZSL.setText("是");
		}

		if (temp.getEmergency_lighting_bright() == 0) {
			tvSFFHBZZD.setText("否");
		} else {
			tvSFFHBZZD.setText("是");
		}
		if (temp.getEscape_sign_add() == 0) {
			tvSSSFFHBZWZ.setText("否");
		} else {
			tvSSSFFHBZWZ.setText("是");
		}
		if (temp.getEscape_sign_num() == 0) {
			tvSSSFFHBZSL.setText("否");
		} else {
			tvSSSFFHBZSL.setText("是");
		}
		if (temp.getEscape_sign_bright() == 0) {
			tvSSSFFHBZZD.setText("否");
		} else {
			tvSSSFFHBZZD.setText("是");
		}
		if (temp.getFloor_indicator_add() == 0) {
			tvLCSFFHBZWZ.setText("否");
		} else {
			tvLCSFFHBZWZ.setText("是");
		}
		if (temp.getFloor_indicator_num() == 0) {
			tvLCSFFHBZSL.setText("否");
		} else {
			tvLCSFFHBZSL.setText("是");
		}

		if (temp.getFloor_indicator_bright() == 0) {
			tvLCSFFHBZZD.setText("否");
		} else {
			tvLCSFFHBZZD.setText("是");
		}

		if (temp.getImage() == null || temp.getImage().size() <= 0) {
			gvXiu.setVisibility(View.GONE);
		} else {
			gvXiu.setVisibility(View.VISIBLE);
			//图片列表
			 List<String> picList = (List<String>) temp.getImage();
			 if (picList != null) {
				 final String [] urls = new String [picList.size()];
					for (int i = 0; i < picList.size(); i++) {
						urls[i] = picList.get(i);
					}
				 
					gvXiu.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
						intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
						intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
						view.getContext().startActivity(intent);
					}
				});
				PicsAdapter picsAdapter = new PicsAdapter(gvXiu.getContext(),picList);
				gvXiu.setAdapter(picsAdapter);
			}
		}
	}
}
